var wait=60;

/**
 @Name: 注册登录等简单校验
 */
layui.define(['layer', 'form'], function(exports){
  var $ = layui.jquery
  ,layer = layui.layer
  ,form = layui.form()
  ,device = layui.device();
  form.verify({
	  password: [/(.+){6,16}$/, '密码必须6到16位'],
	  confirmPwd: function(value) {
			// 获取密码
			var pwd = $("#password").val();
			if(!new RegExp(pwd).test(value)) {
				return '两次输入的密码不一致';
			}
		}
	});
  
  //阻止IE7以下访问
  if(device.ie && device.ie < 8){
    layer.alert('如果您非得使用ie浏览Fly社区，那么请使用ie8+');
  }
  
  layui.focusInsert = function(obj, str){
    var result, val = obj.value;
    obj.focus();
    if(document.selection){ //ie
      result = document.selection.createRange(); 
      document.selection.empty(); 
      result.text = str; 
    } else {
      result = [val.substring(0, obj.selectionStart), str, val.substr(obj.selectionEnd)];
      obj.focus();
      obj.value = result.join('');
    }
  };
  
  var gather = {
    //Ajax
    json: function(url, data, success, options){
      var that = this;
      options = options || {};
      data = data || {};
      return $.ajax({
        type: options.type || 'post',
        dataType: options.dataType || 'json',
        data: data,
        url: url,
        success: function(res){
        	success && success(res);
        }, error: function(e){
          options.error || layer.msg('请求异常，请重试', {shift: 6});
        }
      });
    }

//    //计算字符长度
//    ,charLen: function(val){
//      var arr = val.split(''), len = 0;
//      for(var i = 0; i <  val.length ; i++){
//        arr[i].charCodeAt(0) < 299 ? len++ : len += 2;
//      }
//      return len;
//    }
    
    ,form: {}

//    ,escape: function(html){
//      return String(html||'').replace(/&(?!#?[a-zA-Z0-9]+;)/g, '&amp;')
//      .replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/'/g, '&#39;').replace(/"/g, '&quot;');
//    }
//    
//    ,cookie: function(e,o,t){
//      e=e||"";var n,i,r,a,c,p,s,d,u;if("undefined"==typeof o){if(p=null,document.cookie&&""!=document.cookie)for(s=document.cookie.split(";"),d=0;d<s.length;d++)if(u=$.trim(s[d]),u.substring(0,e.length+1)==e+"="){p=decodeURIComponent(u.substring(e.length+1));break}return p}t=t||{},null===o&&(o="",t.expires=-1),n="",t.expires&&("number"==typeof t.expires||t.expires.toUTCString)&&("number"==typeof t.expires?(i=new Date,i.setTime(i.getTime()+864e5*t.expires)):i=t.expires,n="; expires="+i.toUTCString()),r=t.path?"; path="+t.path:"",a=t.domain?"; domain="+t.domain:"",c=t.secure?"; secure":"",document.cookie=[e,"=",encodeURIComponent(o),n,r,a,c].join("");
//    }
    
  };
  var time = function time(o) {
	  if (wait == 0) {
	   o.attr("disabled", false);
	   o.html("获取验证码");
	   wait = 60;
	  } else { 
		o.attr("disabled", true);
	   o.html(wait +"s后重新发送");
	   wait--;
	   setTimeout(function() {
	    time(o)
	   },
	   1000)
	  }
	 }
  // 获取验证码
  $("#getVcBtn").click(function() {
	  var email = $("#username").val();
 	 if(!(/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/ .test(email))){ 
 		 layer.msg("邮箱格式不正确！", {shift: 3, icon: 5, time: 3000});
 	     return false; 
 	 }
 	 time($("#getVcBtn"));
 	 gather.json("/user/vc", {email : email}, function(res) {
 	   	if(res && res.code && res.code == 1) {
 	       	layer.msg(res.msg||"操作成功", {shift: 3, icon: 1, time: 1000});
 	       } else if(res && res.msg) {
 	       	$("#username").focus();
 	       	layer.msg(res.msg||"操作失败", {shift: 3, icon: 5, time: 3000});
 	       } else {
 	       	layer.msg("操作出错", {shift: 3, icon: 5, time: 3000});
 	       }
 	   });
  });
  
  //表单提交
  form.on('submit(*)', function(data){
    var action = $(data.form).attr('action'), button = $(data.elem);
    gather.json(action, data.field, function(res) {
    	if(res && res.code && res.code == 1) {
        	layer.msg(res.msg||"操作成功", {shift: 3, icon: 1, time: 1000},function(){window.location.href = res.action;});
        } else if(res && res.msg) {
        	$("#username").focus();
        	layer.msg(res.msg||"操作失败", {shift: 3, icon: 5, time: 3000});
        } else {
        	layer.msg("操作出错", {shift: 3, icon: 5, time: 3000});
        }
    });
    return false;
  });

  //手机设备的简单适配
  var treeMobile = $('.site-tree-mobile')
  ,shadeMobile = $('.site-mobile-shade')

  treeMobile.on('click', function(){
    $('body').addClass('site-mobile');
  });

  shadeMobile.on('click', function(){
    $('body').removeClass('site-mobile');
  });

  exports('login', gather);

});
